package com.detech.sbom.base.satoken.listener;

import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.listener.SaTokenListener;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.detech.sbom.base.constant.CacheConstants;
import com.detech.sbom.base.redis.RedisUtils;
import com.detech.sbom.base.satoken.enumtype.DeviceType;
import com.detech.sbom.base.satoken.model.LoginUser;
import com.detech.sbom.base.satoken.model.UserOnline;
import com.detech.sbom.base.satoken.utils.LoginHelper;
import com.detech.sbom.base.util.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.Date;

/**
 * 用户行为 侦听器的实现
 *
 */
@Component
public class UserActionListener implements SaTokenListener {

    @Autowired
    private SaTokenConfig tokenConfig;

    /**
     * 每次登录成功时触发
     */
    @Override
    public void doLogin(String loginType, Object loginId, String tokenValue, SaLoginModel loginModel) {
        DeviceType deviceType = LoginHelper.getLoginDeviceType();
        //往Redis中存储在线用户信息
        UserOnline userOnline = new UserOnline();
        UserAgent userAgent = UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent"));
        if(userAgent!=null){
            userOnline.setBrowser(userAgent.getBrowser().getName());
            userOnline.setOs(userAgent.getOs().getName());
        }
        String ip = ServletUtils.getClientIP();
        LoginUser user = LoginHelper.getLoginUser();
        userOnline.setUserId(user.getId());
        userOnline.setIpaddr(ip);
        userOnline.setLoginDevice(deviceType.getDevice());
        userOnline.setLoginTime(new Date());
        userOnline.setTokenId(tokenValue);
        userOnline.setUserName(user.getUsername());
        RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, userOnline, Duration.ofSeconds(tokenConfig.getTimeout()));

        //自定义业务逻辑
        if (deviceType == DeviceType.PC) {
            // PC端 自行根据业务编写
        }
        else if (deviceType == DeviceType.APP) {
            // APP端 自行根据业务编写
        }
        else if (deviceType == DeviceType.XCX) {
            // 小程序端 自行根据业务编写
        }
    }

    /**
     * 每次注销时触发
     */
    @Override
    public void doLogout(String loginType, Object loginId, String tokenValue) {
        RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue);
    }

    /**
     * 每次被踢下线时触发
     */
    @Override
    public void doKickout(String loginType, Object loginId, String tokenValue) {
        RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue);
    }

    /**
     * 每次被顶下线时触发
     */
    @Override
    public void doReplaced(String loginType, Object loginId, String tokenValue) {
        RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue);
    }

    /**
     * 每次被封禁时触发
     */
    @Override
    public void doDisable(String loginType, Object loginId, String service, int level, long disableTime) {
    }

    /**
     * 每次被解封时触发
     */
    @Override
    public void doUntieDisable(String loginType, Object loginId, String service) {
    }

    /** 每次二级认证时触发 */
    @Override
    public void doOpenSafe(String loginType, String tokenValue, String service, long safeTime) {

    }

    /** 每次退出二级认证时触发 */
    @Override
    public void doCloseSafe(String loginType, String tokenValue, String service) {

    }

    /**
     * 每次创建Session时触发
     */
    @Override
    public void doCreateSession(String id) {
    }

    /**
     * 每次注销Session时触发
     */
    @Override
    public void doLogoutSession(String id) {
    }

    /** 每次Token续期时触发 */
    @Override
    public void doRenewTimeout(String tokenValue, Object loginId, long timeout) {

    }


}
